package com.tlxch.controller.teacher;

import com.tlxch.constant.JwtClaimsConstant;
import com.tlxch.pojo.TTeacher;
import com.tlxch.properties.JwtProperties;
import com.tlxch.result.Result;
import com.tlxch.server.teacher.TeacherService;
import com.tlxch.utils.JwtUtil;
import com.tlxch.utils.MD5Utils;
import com.tlxch.vo.TeacherLoginvo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "教师相关接口")
@Slf4j
@RestController
@RequestMapping("/teacher/teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    MD5Utils md5Utils;
    @Autowired
    JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation("教师登录")
    public Result<TeacherLoginvo> tLogin(@RequestBody TTeacher teacher){
        TTeacher teachervo = teacherService.tlogin(teacher);
        String password = md5Utils.MD5Lower(teacher.gettPassword());
        if (teachervo == null) {
            return Result.error("没有相关管理员");
        }else if(!teachervo.gettPassword().equals(password)){
            return Result.error("密码错误");
        }
        Map<String,Object> claims = new HashMap();
        claims.put(JwtClaimsConstant.USER_ID,teachervo.gettTid());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        System.out.println(jwtProperties.getUserSecretKey());
        TeacherLoginvo teacherLoginvo = TeacherLoginvo.builder()
                .tTeacher(teachervo)
                .token(token)
                .build();
        return Result.success(teacherLoginvo);
    }
}
